package cn.tedu.luban.worker.main.controller;

import cn.tedu.luban.worker.main.converter.WorkerAuditConverter;
import cn.tedu.luban.worker.main.converter.WorkerConverter;
import cn.tedu.luban.worker.main.service.WorkerService;
import cn.tedu.luban.worker.protocol.VO.AdminAuditDetailVO;
import cn.tedu.luban.worker.protocol.VO.AdminWorkerListVO;
import cn.tedu.luban.worker.protocol.VO.FrontWorkerDetailVO;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.bo.FrontWorkerBO;
import cn.tedu.luban.worker.protocol.param.AuditSubmitParam;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import cn.tedu.luban.worker.protocol.query.WorkerListQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.exception.BusinessException;
import com.tedu.inn.protocol.model.Result;
import com.tedu.inn.protocol.pager.PagerResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    /**
     * worker入驻接口
     *
     * @param param 入驻参数
     * @return 入驻成功的workerId
     * <p>
     * 获取userJson的方式： 缺点（破坏内聚）
     * HttpServletRequest request,
     * @RequestHeader("Authorization") String userJson
     */
    @PostMapping("/worker/create")
    public Result<Long> workerCreate(
            @RequestBody WorkerRegistParam param,
            HttpServletRequest request,
            @RequestHeader("Authorization") String userJson
    ) throws BusinessException {
        log.info("worker create param: {}", param);

        String uJson = request.getHeader("Authorization");
        log.info("uJson: {}", uJson);
        log.info("userJson: {}", userJson);

        Long workerId = workerService.workerCreate(param);

        return new Result<>(workerId);
    }


    @Autowired
    private WorkerConverter workerConverter;

    /**
     * worker详情接口
     */
    @GetMapping("/worker/detail")
    public Result<FrontWorkerDetailVO> workerDetail() throws BusinessException {
        //  调用业务逻辑查询师傅详情 BO出参
        FrontWorkerBO frontWorkerBO = workerService.workerDetail();

        //  转换成VO入参
        FrontWorkerDetailVO frontWorkerDetailVO = workerConverter.bo2vo(frontWorkerBO);

        return new Result<>(frontWorkerDetailVO);
    }

    /**
     * 管理员页面 worker列表
     */
    @PostMapping("/admin/worker/aduit")
    public Result<PagerResult<AdminWorkerListVO>> workerAuditList(
            @RequestBody WorkerListQuery query)throws BusinessException{
        // PagerResult 封装5个属性：total, pageSize, pageNo, List, lastPageIndex

        // pageSize, pageNo 就是Query对象中的属性 - WorkerListQuery
        // total, List数据 业务查询 - ListRecordTotalBO
        // lastPageIndex 根据total, pageSize计算得到 - PagerResult - getLastPageIndex() 总页数
        ListRecordTotalBO<AdminWorkerBO> listRecords = workerService.workerAuditList(query);

        // 装配VO
        PagerResult<AdminWorkerListVO> pagerResult = workerConverter.bo2PagerResultVO(listRecords, query);
        return new Result<>(pagerResult);
    }


    @Autowired
    private WorkerAuditConverter workerAuditConverter;

    @PostMapping("/admin/worker/audit/detail")
    public Result<AdminAuditDetailVO> workerAuditDetail(@RequestParam("workerId") Long userId) throws BusinessException {

        log.info("worker audit detail userId: {}", userId);
        // 获取登录用户和参数的userId业务意义不一样，登录用户-管理员 业务参数-审核师傅
        AdminWorkerBO adminWorkerBO = workerService.workerAuditDetail(userId);
        AdminAuditDetailVO vo = workerAuditConverter.bo2vo(adminWorkerBO);
        return new Result<>(vo);
    }

    /**
     * 审核提交接口
     */
    @PostMapping("/admin/worker/audit/save")
    public Result<Long> auditSubmit(@RequestBody AuditSubmitParam param) throws BusinessException {
        // 调用业务逻辑，保存审核结果
        Long auditId = workerService.auditSubmit(param);
        // 返回审核id
        return new Result<>(auditId);
    }





}